﻿using System;
using System.Collections.Generic;
//using Infragistics.Excel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Data;
using CMISLibrary.Libraries;
using CMISLibrary;
using System.Media;
using System.Threading;
//using busQuanLyMatDien;
using BCNPCEntities;
using BaocaoNPC.common;
using System.Windows.Controls.Primitives;
using Ionic.Zip;
namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for XuatNhanSoLieuCongNo.xaml
    /// </summary>
    public partial class XuatNhanSoLieuCongNo : System.Windows.Controls.UserControl
    {
        #region Khai báo biến
        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        string TrangThai = "";
        DataSet dsXuatDulieu;
        DataSet dsThuNganVien;
        DataSet dsBB_GIAOTHU;
        string maloi = "";
        string ma_tngan = "";
        string pasword = "NPCICT2012";
        //bus_TienichNPC.cls_ImportExcel_bus cls = new bus_TienichNPC.cls_ImportExcel_bus();
        #endregion
        public XuatNhanSoLieuCongNo()
        {
            InitializeComponent();
            dsThuNganVien = new DataSet();
            //dsThuNganVien = cls.select_ThuNganVien(strMa_dviqly);
            dsThuNganVien = GlobalServices.Service_NPCUS.select_ThuNganVien(strMa_dviqly);
            if (dsThuNganVien != null) txtThuNganVien.AddItem(new CMIS2_TextBox.AutoCompleteEntry(dsThuNganVien));
            txtThuNganVien.FocusAutoText();
        }

        private void cmdChonDuongDan_Click(object sender, RoutedEventArgs e)
        {
            if (radNhanSoLieu.IsChecked == false && radXuatSoLieu.IsChecked == false)
            {
                SetStatus("ERROR", cmdChonDuongDan, "Chọn xuất dữ liệu hoặc nhận dữ liệu từ file!");
                return;
            }
            if (TrangThai == "XSL") // thực hiện xuất số liệu
            {
                FolderBrowserDialog FBDialog = new FolderBrowserDialog();
                {

                    FBDialog.RootFolder = Environment.SpecialFolder.Desktop;
                    FBDialog.SelectedPath = "c:\\";
                    FBDialog.Description = "Chọn đường dẫn xuất số liệu ra file zip";
                    if (FBDialog.ShowDialog() == DialogResult.OK)
                    {
                        txtDuongDan.Text = FBDialog.SelectedPath;
                    }
                }
            }
            if (TrangThai == "NSL")
            {
                OpenFileDialog fDialog = new OpenFileDialog();
                fDialog.Title = "Chọn file nhận số liệu";
                fDialog.Filter = "Winrar Files(*.rar)|*.rar";
                if (fDialog.ShowDialog() == DialogResult.OK)
                {
                    txtDuongDan.Text = fDialog.FileName.ToString();
                }
            }
        }
        protected void SetStatus(string Begin, System.Windows.Controls.Control ControlA, string strNote)
        {
            switch (Begin)
            {
                case "ERROR":
                    this.Cursor = null;
                    ControlA.Cursor = null;
                    Component.Validate.ValidationManager.showMessOnView(this, 10000, strNote, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
                    break;
                case "OK":
                    lblstatus.Foreground = Brushes.Blue;
                    lblstatus.Content = strNote;
                    this.Cursor = System.Windows.Input.Cursors.Wait;
                    ControlA.Cursor = System.Windows.Input.Cursors.Wait;
                    System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
                    break;
                case "COMPLATE":
                    this.Cursor = null;
                    ControlA.Cursor = null;
                    Component.Validate.ValidationManager.showMessOnView(this, 10000, strNote, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
                    break;
            }
        }
        private void radXuatSoLieu_Checked(object sender, RoutedEventArgs e)
        {
            TrangThai = "XSL";
            cmdXuatNhapDlieu.Content = "Xuất Số liệu";
        }

        private void radNhanSoLieu_Checked(object sender, RoutedEventArgs e)
        {
            TrangThai = "NSL";
            cmdXuatNhapDlieu.Content = "Nhận Số liệu";
        }

        private void CheckGridPtu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                
            }
            catch
            {
                SetStatus("ERROR", btnLayDuLieu, "");
            }
        }
        private void btnLayDuLieu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtThuNganVien.Text.Trim() == "")
                {
                    SetStatus("ERROR", btnLayDuLieu, "Chưa chọn Thu ngân viên");
                    return;
                }
                //lấy mã thu ngân viên
                ma_tngan = catMa(txtThuNganVien.Text.Trim());
                //
                dsBB_GIAOTHU = new DataSet();
                //dsBB_GIAOTHU = cls.select_BB_GIAO_THU(strMa_dviqly, ma_tngan);
                dsBB_GIAOTHU = GlobalServices.Service_NPCUS.select_BB_GIAO_THU(strMa_dviqly,ma_tngan);
                if (dsBB_GIAOTHU != null || dsBB_GIAOTHU.Tables[0].Rows.Count > 0)
                {
                    dgrBB_GIAOTHU.ItemsSource = dsBB_GIAOTHU.Tables[0].DefaultView;
                }
                else
                {
                    SetStatus("ERROR", btnLayDuLieu, "Thu ngân viên không tồn tại hoặc ko có biên bản giao thu!");
                    return;
                }
                // lấy tổng số liệu của thu ngân viên
                dsXuatDulieu = new DataSet();
                List<Int64> intList = new List<Int64>();
                this.Cursor = System.Windows.Input.Cursors.Wait;
                for (int i = 0; i < dsBB_GIAOTHU.Tables[0].Rows.Count; i++)
                {
                    if (dsBB_GIAOTHU.Tables[0].Rows[i]["CHON"].ToString() == "True")
                    {
                        intList.Add(Convert.ToInt64(dsBB_GIAOTHU.Tables[0].Rows[i]["SO_BBGIAO"]));
                    }
                }
                //if (intList.Count() == 0)
                //{
                //    drgHoaDonGThu.ItemsSource = null;
                //    return;
                //}
                //dsXuatDulieu = cls.select_CN_BB_GTHU(strMa_dviqly,catMa(txtThuNganVien.Text.Trim()));
                //if (dsXuatDulieu != null || dsXuatDulieu.Tables["CN_HDON_GTHU"].Rows.Count > 0)
                //{
                //    drgHoaDonGThu.ItemsSource = dsXuatDulieu.Tables["CN_HDON_GTHU"].DefaultView;
                //}
                //DataRowView drw = null;
                //for (int j = 0; j <= drgHoaDonGThu.Items.Count - 1; j++)
                //{
                //    drw = (DataRowView)drgHoaDonGThu.Items[j];
                //    drw["TONG_TIEN"] = Convert.ToDecimal(drw["SO_TIEN"]) + Convert.ToDecimal(drw["TIEN_GTGT"]);
                //}
                this.Cursor = null;
            }
            catch
            { }
        }
        private string catMa(string fullname)
        {
            int i = 0;
            i = fullname.IndexOf("_*_");
            string ma = fullname.Substring(0, i).Trim();
            //string[] path = fullname.Split('_');                // cắt mảng phần tử
            //ptu = path[0].Trim(); // lấy tên phần tử
            return ma;
        }

        private void dgrBB_GIAOTHU_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //int DongCuoi = dgrnhommv.Items.Count;
            //int rowindex = dgrnhommv.SelectedIndex;
            //if (rowindex < 0)
            //{
            //    drgHoaDonGThu.ItemsSource = null;
            //    lblTongSo.Content = "Tổng số 0 phần tử";
            //    lblNguoiGiao.Content = "";
            //    return; 
            //}
            //if (DongCuoi < 0)
            //{
            //    drgHoaDonGThu.ItemsSource = null;
            //    lblTongSo.Content = "Tổng số 0 phần tử";
            //    lblNguoiGiao.Content = "";
            //    return; 
            //}
            DataRowView drv = dgrBB_GIAOTHU.SelectedItem as DataRowView;
            //DataSet dsHD = cls.select_HOADON_GIAO_THU(strMa_dviqly, Convert.ToInt64(drv.Row["SO_BBGIAO"].ToString()));
            DataSet dsHD = GlobalServices.Service_NPCUS.select_HOADON_GIAO_THU(strMa_dviqly,Convert.ToInt64(drv.Row["SO_BBGIAO"].ToString()));
            if (dsHD != null || dsHD.Tables[0].Rows.Count >=0)
            {
                DataTable dt = new DataTable();
                //dt = dsXuatDulieu.Tables["CN_HDON_GTHU"].Select("SO_BBGIAO = '" + drv.Row["SO_BBGIAO"].ToString() + "'").CopyToDataTable();
                //dt.Columns.Add("TONG_TIEN", typeof(Decimal));
                //DataRow[] dr = dt.Select();
                //Decimal tongtien = 0;
                //if (dt.Rows.Count > 0)
                //{
                //    for (int i = 0; i < dt.Rows.Count; i++)
                //    {
                //        dr[i].SetField("TONG_TIEN", Convert.ToDecimal(dr[i]["SO_TIEN"]) + Convert.ToDecimal(dr[i]["TIEN_GTGT"]));
                //        tongtien = tongtien + Convert.ToDecimal(dr[i]["TONG_TIEN"]);
                //    }
                //}
                drgHoaDonGThu.ItemsSource = dsHD.Tables[0].DefaultView;
                //lblTongSo.Content = "Tổng số tiền:" + tongtien + "; " + "Tổng số hóa đơn:" + dt.Rows.Count;
                //DataTable dtt = dsBB_GIAOTHU.Tables[0].Copy();
                //dtt.DefaultView.RowFilter = "MA_TNGAN ='" + drv.Row["MA_TNGAN"].ToString() + "'";
                //lblNguoiGiao.Content = dtt.DefaultView[0]["TEN_NVIEN"].ToString();
            }
        }

        private void cmdXuatNhapDlieu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (radNhanSoLieu.IsChecked == true) // nhận số liệu
                {
     
                }
                if (radXuatSoLieu.IsChecked == true) // xuất số liệu
                {
                    
                    // creat filenam
                    string foldername = txtDuongDan.Text + @"\CMIS2" + strMa_dviqly + System.DateTime.Now.Year.ToString()
                                                                 + System.DateTime.Now.Month.ToString()
                                                                 + System.DateTime.Now.Day.ToString()
                                                                 + System.DateTime.Now.Hour.ToString()
                                                                 + System.DateTime.Now.Minute.ToString()
                                                                 + System.DateTime.Now.Second.ToString();
                   
                    // kiểm tra xem có dữ liệu xuất ko
                    if (dsBB_GIAOTHU.Tables[0].Rows.Count == 0)
                    {
                        SetStatus("ERROR", cmdXuatNhapDlieu, "Không có dữ liệu xuất!");
                        return;
                    }
                    dsXuatDulieu = new DataSet();
                    //dsXuatDulieu = cls.select_CN_BB_GTHU(strMa_dviqly, catMa(txtThuNganVien.Text.Trim()), dsBB_GIAOTHU);
                    dsXuatDulieu = GlobalServices.Service_NPCUS.select_CN_BB_GTHU(strMa_dviqly, catMa(txtThuNganVien.Text.Trim()), dsBB_GIAOTHU);
                    TootsNPC.compression.DsWriteXmlToZip(dsXuatDulieu, foldername, pasword);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Error during Zip operation!", "Alert");
            }
        }
        public void write_file(DataSet ds, string duongdan, ref string maloi)
        {
            try
            {
                //DataTable dt = new DataTable();
                //dt.WriteXml(@duongdan);
                ds.WriteXml(@duongdan);
            }
            catch (Exception ex)
            { maloi = ex.ToString(); }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
